package com.dodock.banglapapers.server.helper;

import java.util.HashMap;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListBanglanews24 implements DDNNewsListInterface {
	
	private String BASE = "http://www.banglanews24.com/index.php";
	private String CATEGORIZEDNEWS = "http://www.banglanews24.com/categorizednews.php?newtype=";
	private String SPECIALCATEGORY = "http://www.banglanews24.com/specialcategory.php?"; 
	private String LIFESTYLE = "http://www.banglanews24.com/LifeStyle/categorinews.php?";//categoryid=6&page=1
	HashMap<String, String> newsMap;
	@Override
	public String GenerateNewsList(String categoryid) {
		newsMap = new HashMap<String, String>();
		try {
			String url = "";
			if (categoryid.equalsIgnoreCase("latest")) {
				url = BASE;
			} else if(categoryid.contains("newtype")){
				url = SPECIALCATEGORY + categoryid;
			} else if (categoryid.contains("categoryid")) {
				url = LIFESTYLE + categoryid +"&page=1";
			} else{
				url = CATEGORIZEDNEWS + categoryid;
			}
			return getNewsList(url, categoryid);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}
	
	private String getNewsList(String url, String categoryid) {
		JsonArray array = new JsonArray();
		
		Document doc;
		try {
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(10000).execute();
			doc = response.parse();
			if (categoryid.equalsIgnoreCase("latest")) {
				parseLeadNews(array, doc);
			} else if(categoryid.contains("newtype")){
				parseSpecialcategoryNews(array, doc);
			} else if (categoryid.contains("categoryid")) {
				parseLifeStyleNews(array, doc);
			} else{
				pareseCategorizedNews(array, doc);
			}
			newsMap.clear();
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}
	
	private void parseLifeStyleNews(JsonArray array, Document doc) {
		Elements leadElements = doc.select("div[id=content_middle]");
		for(int i = 0; i < leadElements.size() - 1 ; i++){
			parseElement(leadElements.get(i), array, true);
		}
	}

	private void parseSpecialcategoryNews(JsonArray array, Document doc) {
		Elements leadElements = doc.select("div[class=contentdiv]");
		for(Element element : leadElements){
			parseElement(element, array, false);
		}
		
		Elements extraNews = doc.select("td[class=TdCo2l]");
		for(Element element : extraNews){
			parseElement(element, array, false);
		}
	}

	private void pareseCategorizedNews(JsonArray array, Document doc) {
		Element tdNewsAll = doc.select("td[class=TdNewsAll]").first();
		Elements tdDetails = tdNewsAll.select("td[class=TdDetails]");
		Elements leadElements = tdDetails.select("td").eq(2);
		if(!leadElements.isEmpty()){
			parseElement(leadElements.first(), array, false);
		}

		leadElements = tdDetails.select("td[class=TdCol]");
		for(Element leadElement : leadElements){
			parseElement(leadElement, array, false);
		}
		
		Element rightElementObj = doc.select("div[class=DHead]").first();
		Elements rightElements = rightElementObj.select("a");
		//for(Element element : rightElements){
		for(int i = 0; i < rightElements.size() - 1 ; i++){
			Element element = rightElements.get(i);
			String id;
			String title;
			if(element != null){
				id = element.attr("href");
				title = element.text();
				if(!newsMap.containsKey(title)){
					JsonObject obj = new JsonObject();
					obj.addProperty("title", Settings.replaceExtras(title));
					obj.addProperty("id", id);
					array.add(obj);
					System.out.println("title : "+title);
					newsMap.put(title, title);
				}
			}
		}
	}
	
	private void parseElement(Element element, JsonArray array, boolean isLifeStyle){
		String image;
		JsonObject obj = new JsonObject();
		Elements imgElements = element.select("img");
		if(!imgElements.isEmpty()){
			image = imgElements.first().attr("src");
			if(isLifeStyle){
				image = "http://www.banglanews24.com/LifeStyle/" + image;
			}
			obj.addProperty("image", image);
		}
		String id;
		String title;
		Elements detialsElemntObj = element.select("a");
		if(!detialsElemntObj.isEmpty()){
			id = detialsElemntObj.first().attr("href");
			title = detialsElemntObj.first().text();
			if(!newsMap.containsKey(title)){
				obj.addProperty("title", Settings.replaceExtras(title));
				newsMap.put(title, title);
				System.out.println("title : "+title);
				if(isLifeStyle){
					id = "____"+id;
				}
				obj.addProperty("id", id);
				array.add(obj);
			}
		}
	}

	private void parseLeadNews(JsonArray array, Document doc){
		Elements mastHeads = null;
		mastHeads = doc
				.select("div[class=D01MainStory]");
		if(!mastHeads.isEmpty()){
			mastHeads = mastHeads.select("a");
			for (Element element : mastHeads) {
				if(element != null){
					boolean isNewNews = false;
					JsonObject obj = new JsonObject();
					String id = element.attr("href");
					if(isNewNews){
						Elements leadNewsElements = element.select("img");
						if(!leadNewsElements.isEmpty()){
							Element newsElement = leadNewsElements.first();
							String img = newsElement.attr("src");
							String title = newsElement.attr("alt");
							obj.addProperty("image", img);
							obj.addProperty("title", title);
							if(!newsMap.containsKey(title)){
								isNewNews = true;
								obj.addProperty("id", id);
								System.out.println("title : "+title);
								newsMap.put(title, title);
								array.add(obj);
							}
						}
					}
				}
			}
		}
		
		mastHeads = doc.select("div[class=DSpecialTop]");
		for(Element newsElement : mastHeads){
			parseElement(newsElement, array,  false);
		}

		mastHeads = doc.select("div[class=DSpecialTopR]");
		for(Element newsElement : mastHeads){
			parseElement(newsElement, array,  false);
		}
		
		mastHeads = doc.select("div[id=tab1]");
		Elements elements = mastHeads.select("a");
		for(Element newsElement : elements){
			JsonObject obj = new JsonObject();
			String id = newsElement.attr("href");
			if(!newsMap.containsKey(id)){
				obj.addProperty("id", id);
				obj.addProperty("title", newsElement.text());
				System.out.println("title : "+newsElement.text());
				newsMap.put(newsElement.text(),newsElement.text());
				array.add(obj);
			}
		}
	}
}
